package com.eclipse.e20200117_dbcp_HikariCP.dbcp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;

public class Test1 {

	public static void main(String[] args) throws Exception {
		//创建配置文件对象
		Properties properties = new Properties();
		
		//配置文件添加到properties对象中
		properties.load(Test1.class.getClassLoader().getResourceAsStream("dbcp.properties"));
		
		//把配置文件传给数据源对象
		BasicDataSource dataSource = BasicDataSourceFactory.createDataSource(properties);
		
		//获取链接
		Connection connection = dataSource.getConnection();
		
		PreparedStatement statement = connection.prepareStatement("select 1");
//		TimeUnit.SECONDS.sleep(10);
		ResultSet resultSet = statement.executeQuery();
		if (resultSet.next()) {
			System.out.println("有");
		}
		connection.close();
		dataSource1();
	}

	public static void dataSource1() throws Exception {
		BasicDataSource dataSource = new BasicDataSource();
		dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
		dataSource.setUrl(
				"jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&rewriteBatchedStatements=true");
		dataSource.setUsername("root");
		dataSource.setPassword("qq123123");

		Connection connection = dataSource.getConnection();

		PreparedStatement statement = connection.prepareStatement("select 1");

		ResultSet resultSet = statement.executeQuery();

		if (resultSet.next()) {
			System.out.println("有");
		}
	}

}
